import { Tool } from 'src/store/tool/state'
import { genWidgetId } from 'src/utils/widget'

export type AppFormData = {
  _id?: string,
  name: string,
  icon?: string,
  permalink?: string,
}

export type AppWidgetData = {
  id?: string,
  type: number, // 应用类型 eg. 1.推荐工具 2.自定义URL
  appData: {
    _id?: string,
    name: string,
    icon?: string,
    permalink?: string,
    // 其他数据
    // ...
  },
  // 统计信息
  clickCount: number,
  // 组件布局属性
  x?: number,
  y?: number,
  w?: number,
  h?: number,
  // 数据信息
  createdAt: Date, // 创建时间
  updatedAt: Date, // 更新时间
}

export type Widget = {
  id: string,
  // 组件布局属性
  x: number,
  y: number,
  w: number,
  h: number,
  noResize?: boolean,
  static?: boolean,
  minH?: number,
  minW?: number,
  maxH?: number,
  maxW?: number,
  // 组件基础属性
  type: string,
  deletable?: boolean,
  // 组件内容
  title?: string,
  // 应用组件内容
  apps?: AppWidgetData[],
  // 组件内容数据
  data?: unknown,
}
export type WidgetData = Widget;

export interface WidgetStateInterface {
  widgets: Widget[],
  apps: AppWidgetData[],
  defaultWidgets: Widget[],
  collectionApps: Tool[],
  options: {
    float: boolean,
    initialized: boolean,
  },
}

export const defaultWidgetOptions = {
  float: false,
  initialized: false
}

const createNewAppWidgetData = (app: AppFormData): AppWidgetData => {
  return {
    id: genWidgetId('APP'),
    type: 2,
    appData: {
      name: app.name,
      icon: app.icon,
      permalink: app.permalink
    },
    clickCount: 0,
    createdAt: new Date(),
    updatedAt: new Date()
  }
}

const defaultAppDataList = [
  createNewAppWidgetData({
    name: '51提效应用推荐',
    icon: 'https://workbench-1301845716.cos.ap-nanjing.myqcloud.com/icon-android-icon-192x192.png',
    permalink: 'https://51tixiao.com/apps'
  }),
  createNewAppWidgetData({
    name: '51提效知识库',
    icon: 'https://workbench-1301845716.cos.ap-nanjing.myqcloud.com/icon-android-icon-192x192.png',
    permalink: 'https://51tixiao.com/km'
  })
]

function state (): WidgetStateInterface {
  return {
    widgets: [],
    apps: defaultAppDataList || [],
    defaultWidgets: [
      {
        id: 'SEARCH_BAR',
        // 组件布局属性
        x: 2,
        y: 0,
        w: 8,
        h: 1,
        maxH: 1,
        // 组件基础属性
        type: 'SearchBarWidget',
        deletable: false
      },
      {
        id: 'WEATHER',
        // 组件布局属性
        x: 0,
        y: 0,
        w: 2,
        h: 1,
        maxH: 1,
        noResize: false,
        // 组件基础属性
        type: 'WeatherWidget',
        deletable: false,
        data: {
          province: {
            id: '10',
            name: '河南'
          },
          city: {
            id: '148',
            name: '郑州',
            cityCode: '101180101'
          }
        }
      },
      {
        id: 'APP_GROUP_51TIXIAO',
        x: 0,
        y: 1,
        w: 3,
        h: 3,
        type: 'AppGroupWidget',
        title: '51提效工具组',
        apps: defaultAppDataList
      }
    ],
    collectionApps: [],
    options: defaultWidgetOptions
  }
}

export default state
